
/**------------------------------------------------------------------------------

	Program	: Franscriptor
	.
	Date	: 18/05/11

	Author	: Miguel Crespo Balzac (MCB)

		DIGISCRIB :
		16, rue des Montils
		37520 La Riche
		France
		contact[at]digiscrib[dot]com
		http://www.digiscrib.com

	Version	: 1.0

	Transforming texts automatically from diplomatic to what we
	call digital "Cultural Heritage" transcription.
	The conversion implies the expansion of abbreviations (untilding),
	according to the combinations authorized in Renaissance French,
	the enforcement of rules of substitution for i/j, u/v (dissimilation).

	This software is subject to the GNU General Public License Version 3

------------------------------------------------------------------------------**/



/*--------------------------------*/
/*--	IMPORT JAVA CLASSES	--*/
/*--------------------------------*/
import java.io.*;
import java.lang.*;
import java.util.*;



	/*------------------------*/
	/*--	MAIN CLASS	--*/
	/*------------------------*/
public class Franscriptor
{
 static final int	VERSION=1;
 static final int	RELEASE=0;
 static Corrector	DissimCorr=new Corrector();
 static Corrector	UntildeCorr=new Corrector();


	/*--	Usage function	--*/
	/*------------------------*/
 public static void Creator()
  {
   System.out.println("\nCopyright (C) 2011 DIGISCRIB LLC");
   System.out.println("http://www.digiscrib.com");
   System.out.println("contact@digiscrib.com");
   System.out.println("Author : Miguel Crespo Balzac (MCB)");
   System.out.println("This software is subject to the GNU General Public License Version 3");
  }

	/*--	Usage function	--*/
	/*------------------------*/
 public static void Usage()
  {
//    System.out.println("\nCopyright (C) 2011 DIGISCRIB LLC");
//    System.out.println("http://www.digiscrib.com");
//    System.out.println("contact@digiscrib.com");
//    System.out.println("Author : Miguel Crespo Balzac (MCB)");
//    System.out.println("This software is subject to the GNU General Public License Version 3");
   Creator();
   System.out.println("\nUsage : Franscriptor : [OPTIONS] -i INFILE [-o OUTFILE]\n");
   System.out.println("\t-d | -dissim\t\tDissimile text");
   System.out.println("\t-dissimfile\t\tDissimilation rules file path");
   System.out.println("\t-h | -help\t\tShow this help");
   System.out.println("\t-i | -in\t\tInput file name");
   System.out.println("\t-incharset\t\tInput file charset");
   System.out.println("\t-o | -out\t\tOutput file name");
   System.out.println("\t-outcharset\t\tOutput file charset");
   System.out.println("\t-showrules\t\tShow untilding and dissimilation rules");
   System.out.println("\t-u | -untilde\t\tUntilde text");
   System.out.println("\t-untildefile\t\tUntilding rules file path");
   System.out.println("\t-v | -version\t\tDisplay version and exit");
   System.out.println("\t-verbose\t\tDisplay updates and errors");
  }



		/*--	MAIN FUNCTION	--*/
		/*------------------------*/
 public static void main(String[] args)
  {
   boolean		Untilding=false, Dissimilation=false, ShowRules=false, Verbose=false;
   int			Cntr=0, Char=0;
   String		InCharset="UTF-8", OutCharset="UTF-8", InFileName=null, OutFileName=null;
   String		DissimPath=null, UntildePath=null, Text=null, NewText=null;
   StringBuffer		Buffer=new StringBuffer();
   File			InFile=null;
   FileInputStream	FileInput=null;
   InputStreamReader	FileRead=null;
   Reader		Rdr=null;
   FileOutputStream	FileOutput=null;
   OutputStreamWriter	FileWrite=null;



	/*--	Read program options	--*/
	/*--------------------------------*/
   for(Cntr=0; Cntr < args.length; Cntr++)
    {
     if(args[Cntr].compareTo("-i") == 0 || args[Cntr].compareTo("-in") == 0)
      {		/*--	Input file name	--*/
       Cntr++;
       if(Cntr < args.length && args[Cntr] != null && args[Cntr].length() > 0)
        { InFileName = args[Cntr]; }
      }
     else
     if(args[Cntr].compareTo("-incharset") == 0)
      {		/*--	Input file charset	--*/
       Cntr++;
       if(Cntr < args.length && args[Cntr] != null && args[Cntr].length() > 0)
        { InCharset = args[Cntr]; }
      }
     else
     if(args[Cntr].compareTo("-o") == 0 || args[Cntr].compareTo("-out") == 0)
      {		/*--	Output file name	--*/
       Cntr++;
       if(Cntr < args.length && args[Cntr] != null && args[Cntr].length() > 0)
        { OutFileName = args[Cntr]; }
      }
     else
     if(args[Cntr].compareTo("-outcharset") == 0)
      {		/*--	Output file charset	--*/
       Cntr++;
       if(Cntr < args.length && args[Cntr] != null && args[Cntr].length() > 0)
        { OutCharset = args[Cntr]; }
      }
     else
     if(args[Cntr].compareTo("-d") == 0 || args[Cntr].compareTo("-dissim") == 0)
      {		/*--	Set text dissimilation	--*/
       Dissimilation = true;
      }
     else
     if(args[Cntr].compareTo("-dissimfile") == 0)
      {		/*--	Dissimilation rules file path	--*/
       Cntr++;
       if(Cntr < args.length && args[Cntr] != null && args[Cntr].length() > 0)
        { DissimPath = args[Cntr]; }
      }
     else
     if(args[Cntr].compareTo("-u") == 0 || args[Cntr].compareTo("-untilde") == 0)
      {		/*--	Set text untilding	--*/
       Untilding = true;
      }
     else
     if(args[Cntr].compareTo("-untildefile") == 0)
      {		/*--	Untilding rules file path	--*/
       Cntr++;
       if(Cntr < args.length && args[Cntr] != null && args[Cntr].length() > 0)
        { UntildePath = args[Cntr]; }
      }
     else
     if(args[Cntr].compareTo("-s") == 0 || args[Cntr].compareTo("-showrules") == 0)
      {		/*--	Show rules	--*/
       ShowRules = true;
      }
     else
     if(args[Cntr].compareTo("-h") == 0 || args[Cntr].compareTo("-help") == 0)
      {		/*--	Display usage information	--*/
       Usage();
       System.exit(0);
      }
     else
     if(args[Cntr].compareTo("-v") == 0 || args[Cntr].compareTo("-version") == 0)
      {		/*--	Display version information	--*/
       Creator();
       System.out.println("Version : "+VERSION+"."+RELEASE);
       System.exit(0);
      }
    } /*-- args.length for end --*/



	/**--	Check for untilding or dissimilation	--*/
   if(!Dissimilation && !Untilding)
    {
     System.err.println("No Untilding/Dissimilation. What do you to do ?\nUse option [-d] for dissimilation and option [-u] for untilding");
     System.exit(1);
    }



	/**--	Check for dissimilation	--*/
	/**-------------------------------*/
   if(Dissimilation)
    {
	/**--	Check for dissimilation rules file path	--*/
     if(DissimPath == null || DissimPath.length() == 0)
      {
       System.err.println("No dissimilation rules file path !");
       System.exit(1);
      }

	/**--	Check for dissimilation rules file	--*/
     InFile = new File(DissimPath);
     if(!InFile.exists())
      {
       System.err.println("Dissimilation rules file not found !");
       System.exit(1);
      }

	/**--	Open dissimilation rules file	--*/
     try
      {
       FileInput = new FileInputStream(InFile);
       FileRead = new InputStreamReader(FileInput, "UTF-8");
      }
     catch(Exception Error)
      {
       System.err.println("Cannot open dissimilation rules file");
       System.exit(1);
      }

	/**--	Read dissimilation rules file	--*/
     try
      {
       Rdr = new BufferedReader(FileRead);
       while((Char = Rdr.read()) > -1) { Buffer.append((char)Char); }
       Text = Buffer.toString();
       Buffer = null;
      }
     catch(Exception Error)
      {
       System.err.println("Cannot read dissimilation rules file");
       System.exit(1);
      }

	/**--	Close dissimilation rules file	--*/
     try
      { FileInput.close(); }
     catch(Exception Error)
      {
       System.err.println("Cannot close dissimilation rules file");
       System.exit(1);
      }
     DissimCorr.ReadCorrectorData(Text);
     Buffer=new StringBuffer();
     Text = null;
    } /**-- dissimilation if end --*/

	/*--	Check for display dissimilation rules	--*/
   if(ShowRules) { System.out.println("\nDissimilation rules :\n"); DissimCorr.ShowCorrRules(); }


	/**--	Check for untilding	--*/
	/**-------------------------------*/
   if(Untilding)
    {
	/**--	Check for untilding rules file path	--*/
     if(UntildePath == null || UntildePath.length() == 0)
      {
       System.err.println("No untilding rules file path !");
       System.exit(1);
      }

	/**--	Check for untilding rules file	--*/
     InFile = new File(UntildePath);
     if(!InFile.exists())
      {
       System.err.println("Untilding rules file not found !");
       System.exit(1);
      }

	/**--	Open untilding rules file	--*/
     try
      {
       FileInput = new FileInputStream(InFile);
       FileRead = new InputStreamReader(FileInput, "UTF-8");
      }
     catch(Exception Error)
      {
       System.err.println("Cannot open untilding rules file");
       System.exit(1);
      }

	/**--	Read untilding rules file	--*/
     try
      {
       Rdr = new BufferedReader(FileRead);
       while((Char = Rdr.read()) > -1) { Buffer.append((char)Char); }
       Text = Buffer.toString();
       Buffer = null;
      }
     catch(Exception Error)
      {
       System.err.println("Cannot read untilding rules file");
       System.exit(1);
      }

	/**--	Close untilding rules file	--*/
     try
      { FileInput.close(); }
     catch(Exception Error)
      {
       System.err.println("Cannot close untilding rules file");
       System.exit(1);
      }
     UntildeCorr.ReadCorrectorData(Text);
     Buffer=new StringBuffer();
     Text = null;
    } /**-- untilding if end --*/

	/*--	Check for display untilding rules and exit	--*/
   if(ShowRules) { System.out.println("\nUntilding rules :\n"); UntildeCorr.ShowCorrRules(); System.exit(0); }



	/**--	Check for input file name	--*/
   if(InFileName == null || InFileName.length() == 0)
    {
     System.err.println("No input text file name");
     System.exit(1);
    }



	/**--	Check for input file	--*/
   InFile = new File(InFileName);
   if(!InFile.exists())
    {
     System.err.println("Input file not found !");
     System.exit(1);
    }

	/**--	Open document file	--*/
   try
    {
     FileInput = new FileInputStream(InFile);
     FileRead = new InputStreamReader(FileInput, InCharset);
    }
   catch(Exception Error)
    {
     System.err.println("Cannot open document");
     System.exit(1);
    }

	/**--	Read document	--*/
   try
    {
     Rdr = new BufferedReader(FileRead);
     while((Char = Rdr.read()) > -1) { Buffer.append((char)Char); }
     Text = Buffer.toString();
     Buffer = null;
    }
   catch(Exception Error)
    {
     System.err.println("Cannot read document");
     System.exit(1);
    }

	/**--	Close document	--*/
   try
    { FileInput.close(); }
   catch(Exception Error)
    {
     System.err.println("Cannot close document");
     System.exit(1);
    }


	/**--	Check for text	--*/
   if(Text == null || Text.length() == 0)
    {
     System.err.println("No text to treat !");
     System.exit(1);
    }


	/**--	Normalize text	--*/
   NewText = "";
   for(Cntr=0; Cntr < Text.length(); Cntr++)
    {
     switch(Text.charAt(Cntr))
      {
       case ' ':
		if(((Cntr + 1) < Text.length() &&
		   (Text.charAt(Cntr + 1) == ' ' ||
		    Text.charAt(Cntr + 1) == '\r' ||
		    Text.charAt(Cntr + 1) == '\n')) ||
		   (Cntr + 1) == Text.length())
		 { break; }
       default:
		NewText = NewText+Text.charAt(Cntr);
		break;
      }
    }
   Text = NewText;
   NewText = null;


	/**--	Untilding the document	--*/
   if(Untilding) { NewText = UntildeCorr.CorrectDocument(Text, Verbose); } else { NewText = Text; }


	/**--	Dissimile the document	--*/
   if(Dissimilation) { NewText = DissimCorr.CorrectDocument(NewText, Verbose); }


	/**--	Check for new text	--*/
   if(NewText == null || NewText.length() == 0)
    {
     System.err.println("No new text treated");
     System.exit(1);
    }


	/**--	Check for output file name	--*/
   if(OutFileName == null || OutFileName.length() == 0)
    {
     System.out.println(NewText);
    }
   else
    {
    	/*--	Open output file	--*/
     try
      {
       FileOutput = new FileOutputStream(OutFileName);
       FileWrite = new OutputStreamWriter(FileOutput, OutCharset);
      }
     catch(Exception Error)
      {
       System.err.println("Cannot open output file");
       System.exit(1);
      }

	/*--	Write output file	--*/
     try
      { FileWrite.write(NewText, 0, NewText.length()); }
     catch(Exception Error)
      {
       System.err.println("Cannot write output file");
       System.exit(1);
      }

	/*--	Close output file	--*/
     try
      { FileWrite.close(); }
     catch(Exception Error)
      {
       System.err.println("Cannot close output file");
       System.exit(1);
      }
    }


   if(Verbose) { System.out.println("Done !!!"); }
   System.exit(0);
  } /*-- main function end --*/

} /*-- Class Franscriptor end --*/
